import React from 'react';
import type { MenuProps } from 'antd';
import { 
  DashboardOutlined, 
  UserOutlined, 
  TeamOutlined,
  ApartmentOutlined,
  HeartOutlined
} from '@ant-design/icons';

/**
 * 菜单项类型定义
 */
export interface MenuItem {
  key: string;
  icon?: React.ReactNode;
  label: string;
  path?: string; // 路由路径
  children?: MenuItem[];
}

/**
 * 菜单配置数据
 * 使用声明式配置，便于维护和扩展
 */
export const menuConfig: MenuItem[] = [
  {
    key: '/dashboard',
    icon: <DashboardOutlined />,
    label: '驾驶舱',
    path: '/dashboard',
  },
  {
    key: '/user',
    icon: <UserOutlined />,
    label: '用户管理',
    path: '/user',
  },
  {
    key: 'department',
    icon: <TeamOutlined />,
    label: '部门管理',
    children: [
      {
        key: '/department/structure',
        icon: <ApartmentOutlined />,
        label: '组织架构',
        path: '/department/structure',
      },
    ]
  }
];

/**
 * 将菜单配置转换为Antd Menu组件需要的items格式
 */
export const convertToMenuItems = (config: MenuItem[]): MenuProps['items'] => {
  return config.map(item => ({
    key: item.key,
    icon: item.icon,
    label: item.label,
    children: item.children ? convertToMenuItems(item.children) : undefined,
  }));
};

/**
 * 创建路径映射表，用于快速查找菜单项对应的路由
 */
export const createPathMap = (config: MenuItem[]): Record<string, string> => {
  const pathMap: Record<string, string> = {};
  
  const traverse = (items: MenuItem[]) => {
    items.forEach(item => {
      if (item.path) {
        pathMap[item.key] = item.path;
      }
      if (item.children) {
        traverse(item.children);
      }
    });
  };
  
  traverse(config);
  return pathMap;
};

// 导出路径映射表
export const menuPathMap = createPathMap(menuConfig);